package com.petecat.interchan.base.project.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.petecat.interchan.base.project.entity.EsServer;
import com.petecat.interchan.base.project.entity.EsServerHistory;
import com.petecat.interchan.base.project.entity.EsServerSoftWareDTO;
import com.petecat.interchan.base.project.entity.EsSoftPercent;
import com.petecat.interchan.base.project.entity.EsSofts;
import com.petecat.interchan.base.project.entity.ServerInfoDTO;
import com.petecat.interchan.base.project.service.IProjectService;
import com.petecat.interchan.core.constans.Global;
import com.petecat.interchan.core.controller.BaseController;
import com.petecat.interchan.protocol.GlobalHeader;
import com.petecat.interchan.protocol.Result;
import com.petecat.interchan.protocol.base.project.dto.BsProjectAddDTO;
import com.petecat.interchan.protocol.base.project.dto.BsProjectQryDTO;
import com.petecat.interchan.protocol.base.project.dto.BsProjectUpdDTO;
import com.petecat.interchan.protocol.base.project.vo.BsProjectQryVO;
import com.petecat.interchan.protocol.base.project.vo.CompanySelectVO;
import com.petecat.interchan.protocol.base.project.vo.ProjectTreeVO;
import com.petecat.interchan.protocol.data.PageVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@Api("项目管理")
@RequestMapping("/base/project")
public class ProjectController extends BaseController{
		
	@Autowired
	private IProjectService projectService;
	
	@SuppressWarnings("unchecked")
	@ApiOperation("分页查询项目信息")
	@GetMapping("/queryProjectByPage")
	public Result<PageVO<BsProjectQryVO>> queryProjectByPage(@ModelAttribute BsProjectQryDTO projectQryDTO,
			@ApiIgnore @RequestHeader(name = Global.GLOBAL_HEADER, required = false) String header) {
		GlobalHeader userInfo = this.getUserInfo(header, true);
		projectQryDTO.setUserId(userInfo.getUserId());
		PageVO<BsProjectQryVO> pageVO = projectService.queryProjectByPage(projectQryDTO);
		return (Result<PageVO<BsProjectQryVO>>) Result.ok(pageVO);
	}

	@ApiOperation(value = "新增项目基本信息", notes = "新增")
	@PostMapping(value = "/saveProject")
	public Result<?> saveProject(@RequestBody BsProjectAddDTO bsProjectAddDTO,
			@ApiIgnore @RequestHeader(name = Global.GLOBAL_HEADER, required = false) String header) {
		GlobalHeader userInfo = this.getUserInfo(header, true);
		projectService.saveProject(bsProjectAddDTO, userInfo.getUserId());
		return Result.ok();
	}

	@ApiOperation("修改项目基本状态")
	@DeleteMapping("/removeProjectById")
	public Result<?> removeProjectById(@RequestParam String projectId,@RequestParam String state,
			@ApiIgnore @RequestHeader(name = Global.GLOBAL_HEADER, required = false) String header) {
		projectService.updateProjectById(projectId,state);
		return Result.ok();
	}
	
	@ApiOperation("删除项目基本信息")
	@DeleteMapping("/deleteProjectById")
	public Result<?> deleteProjectById(@RequestParam String projectId,
			@ApiIgnore @RequestHeader(name = Global.GLOBAL_HEADER, required = false) String header) {
		projectService.deleteProjectById(projectId);
		return Result.ok();
	}
	
	@ApiOperation("通过ID刷新TOKEN")
	@DeleteMapping("/updProjectTokenById")
	public Result<?> updProjectTokenById(@RequestParam String projectId,@RequestParam String projectToken,
			@ApiIgnore @RequestHeader(name = Global.GLOBAL_HEADER, required = false) String header) {
		GlobalHeader userInfo = this.getUserInfo(header, true);
		projectService.updProjectTokenById(projectId,projectToken,userInfo.getUserId());
		return Result.ok();
	}
	
	@ApiOperation("修改项目基本信息")
	@PutMapping("/modifyProjectById")
	public Result<?> modifyProjectById(@RequestBody BsProjectUpdDTO bsProjectUpdDTO,
			@ApiIgnore @RequestHeader(name = Global.GLOBAL_HEADER, required = false) String header) {
		GlobalHeader userInfo = this.getUserInfo(header, true);
		projectService.modifyProjectById(bsProjectUpdDTO, userInfo.getUserId());
		return Result.ok();
	}
	
	
	@SuppressWarnings("unchecked") 
	@ApiOperation("下拉查询启用项目信息")
	@GetMapping("/queryProjectSelect")
	public Result<List<CompanySelectVO>> queryProjectSelect(@ApiIgnore @RequestHeader(name = Global.GLOBAL_HEADER, required = false) String header) {
		GlobalHeader userInfo = this.getUserInfo(header, true);
		List<CompanySelectVO> list = projectService.queryProjectSelect(userInfo.getUserId());
		return  (Result<List<CompanySelectVO>>) Result.ok(list);
	}
	
	@SuppressWarnings("unchecked") 
	@ApiOperation("查询项目下拉树")
	@GetMapping("/queryProjectByTree")   
	public Result<PageVO<ProjectTreeVO>> queryProjectByTree(@ModelAttribute BsProjectQryDTO projectQryDTO,@ApiIgnore @RequestHeader(name = Global.GLOBAL_HEADER, required = false) String header) {
		GlobalHeader userInfo = this.getUserInfo(header, true);
		PageVO<ProjectTreeVO> pageVO = projectService.queryProjectByTree(projectQryDTO,userInfo.getUserId());
		return (Result<PageVO<ProjectTreeVO>>) Result.ok(pageVO);
	}
	
	@SuppressWarnings("unchecked") 
	@ApiOperation("通过Token查询项目所在的服务器信息")
	@GetMapping("/queryServerInfo")
	public Result<List<EsServer>> queryServerInfo(String token,@ApiIgnore @RequestHeader(name = Global.GLOBAL_HEADER, required = false) String header){
		List<EsServer> esServerList = projectService.queryServerInfo(token);
		return (Result<List<EsServer>>) Result.ok(esServerList);
	}
	
	@SuppressWarnings("unchecked") 
	@ApiOperation("通过Token和Mac查询服务器使用率%圖表")
	@GetMapping("/queryServerPercent")  
	public Result<List<EsSoftPercent>> queryServerPercent(@ModelAttribute ServerInfoDTO serverInfoDTO,@ApiIgnore @RequestHeader(name = Global.GLOBAL_HEADER, required = false) String header){
		GlobalHeader userInfo = this.getUserInfo(header, true);
		List<EsSoftPercent> esServerList = projectService.queryServerPercent(serverInfoDTO,userInfo.getUserId());
		return (Result<List<EsSoftPercent>>) Result.ok(esServerList);
	}
	
	@SuppressWarnings("unchecked") 
	@ApiOperation("查询启用和用户自定义首页的项目信息")
	@GetMapping("/queryProjectByCustom")   
	public Result<List<CompanySelectVO>> queryProjectByCustom(@RequestHeader(name = Global.GLOBAL_HEADER, required = false) String header) {
		GlobalHeader userInfo = this.getUserInfo(header, true);
		List<CompanySelectVO> list = projectService.queryProjectByCustom(userInfo.getUserId());
		return  (Result<List<CompanySelectVO>>) Result.ok(list);
	}
	
	@SuppressWarnings("unchecked") 
	@ApiOperation("查询用户自定义项目")
	@GetMapping("/queryCustomByUserId")   
	public Result<List<CompanySelectVO>> queryCustomByUserId(@ApiIgnore @RequestHeader(name = Global.GLOBAL_HEADER, required = false) String header) {
		GlobalHeader userInfo = this.getUserInfo(header, true);
		List<CompanySelectVO> list = projectService.queryCustomByUserId(userInfo.getUserId());
		return  (Result<List<CompanySelectVO>>) Result.ok(list);
	}
	
	
	@SuppressWarnings("unchecked") 
	@ApiOperation("查询用户没有自定义的项目")
	@GetMapping("/queryNoCustom")   
	public Result<List<CompanySelectVO>> queryNoCustom(@RequestParam("ids")String [] ids,@ApiIgnore @RequestHeader(name = Global.GLOBAL_HEADER, required = false) String header) {
		GlobalHeader userInfo = this.getUserInfo(header, true);
		List<CompanySelectVO> list = projectService.queryNoCustom(userInfo.getUserId(),ids);
		return  (Result<List<CompanySelectVO>>) Result.ok(list);
	}
	
	
	@SuppressWarnings("unchecked") 
	@ApiOperation("通过Mac查询服务器历史信息")
	@GetMapping("/queryServerHistory")  
	public Result<PageVO<EsServerHistory>> queryServerHistory(@RequestParam String serverMac,@RequestParam String start,@RequestParam String rows,@ApiIgnore @RequestHeader(name = Global.GLOBAL_HEADER, required = false) String header){
		GlobalHeader userInfo = this.getUserInfo(header, true);
		PageVO<EsServerHistory> esServerList = projectService.EsServerHistory(serverMac,userInfo.getUserId(),start,rows);
		return (Result<PageVO<EsServerHistory>>) Result.ok(esServerList);
	}
	
	@SuppressWarnings("unchecked") 
	@ApiOperation("查询最近的ES服务器软件信息")
	@GetMapping("/queryEsSoftwareInfo")  
	public Result<PageVO<EsSofts>> queryEsSoftwareInfo(@ModelAttribute EsServerSoftWareDTO serverSoftWareDTO,@ApiIgnore @RequestHeader(name = Global.GLOBAL_HEADER, required = false) String header){
		GlobalHeader userInfo = this.getUserInfo(header, true);
		serverSoftWareDTO.setUserId(userInfo.getUserId());
		PageVO<EsSofts> esSoftList = projectService.queryEsSoftwareInfo(serverSoftWareDTO);
		return (Result<PageVO<EsSofts>>) Result.ok(esSoftList);
	}
	
	@ApiOperation("查询所有的项目服务器ES信息并导出Execl")
	@GetMapping("/outputAllServerInfo")
	public ResponseEntity<byte[]> outputAllServerInfo(){
		return projectService.queryAllServerInfo();
	}
	
	@ApiOperation("通过项目ID查询项目名称")
	@GetMapping("/getNameByProjectId")
	public String getNameByProjectId(@RequestParam String peojectId,@ApiIgnore @RequestHeader(name = Global.GLOBAL_HEADER, required = false) String header){
		return projectService.getNameByProjectId(peojectId);
	}
	
}
